package com.twu.biblioteca.helpers;

import java.util.ArrayList;
import java.util.List;

public class Table {

    private List<Integer> limits;
    private StringBuilder table;

    public Table() {
        this.table = new StringBuilder();
    }

    public void addData(ArrayList<String> data){
        StringBuilder line = new StringBuilder();

        for (int i=0; i < data.size(); i++){
            int charLeft = limits.get(i) - data.get(i).length();

            int limitChars = (charLeft%2 == 0) ? charLeft : charLeft-1;
            limitChars /= 2;

            for (int a=0; a < limitChars; a++){
                line.append(" ");
            }

            line.append(data.get(i));

            for (int a=0; a < limitChars; a++){
                line.append(" ");
            }
            if(charLeft%2 != 0){
                line.append(" ");
            }
            if(i != data.size()-1){
                line.append("|");
            }
        }

        line.append("\n");

        this.table.append(line);
    }

    public void addDiv(){
        StringBuilder div = new StringBuilder();

        for (int i=0; i < this.limits.size(); i++){
            for (int a=0; a < this.limits.get(i); a++){
                div.append("-");
            }
            if(i != this.limits.size()-1){
                div.append("*");
            }
        }

        div.append("\n");

        this.table.append(div);
    }

    public List<Integer> getLimits() {
        return limits;
    }

    public StringBuilder getTable() {
        return table;
    }

    public void setLimits(ArrayList<Integer> newLimits){
       this.limits = newLimits;
    }

}
